package com.qf.web.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.aop.aspect.ApiOperationLog;
import com.qf.aop.aspect.ApiOperationLogTitle;
import com.qf.api.sys.dto.OperationLogDTO;
import com.qf.api.sys.entity.SysLoginLogEntity;
import com.qf.api.sys.entity.SysOperationLogEntity;
import com.qf.api.sys.mybatis.page.BaseController;
import com.qf.api.sys.mybatis.page.PageResult;
import com.qf.api.sys.query.LoginLogQuery;
import com.qf.api.sys.query.OperationLogQuery;
import com.qf.api.sys.service.SysLoginLogService;
import com.qf.api.sys.service.SysOperationLogService;
import com.qf.core.enums.common.BusinessTypeEnum;
import com.qf.core.response.ResponseResult;
import com.qf.core.utils.CustomExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: sin
 * @Date: 2025/5/10 21:25
 * @Description: 日志
 **/
@RestController
@Tag(name = "05.日志管理")
@ApiOperationLogTitle("日志模块")
@RequestMapping("/admin/logs")
@Slf4j
public class LogController extends BaseController {

    @Autowired
    private SysLoginLogService loginLogService;

    @Autowired
    private SysOperationLogService operationLogService;

    /**
     * 获取登录日志列表
     **/
    @Operation(summary = "01.获取登录日志列表")
    @GetMapping("/login")
    public ResponseResult<PageResult<SysLoginLogEntity>> getLoginLog(LoginLogQuery query) {
        startPage();
        List<SysLoginLogEntity> sysLoginLogs = loginLogService.list(query.toQueryWrapper());
        PageResult<SysLoginLogEntity> data = getData(sysLoginLogs);
        return ResponseResult.success(data);
    }

    @Operation(summary = "02.删除登录日志")
    @ApiOperationLog(desc = "删除登录日志", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/login")
    public ResponseResult<Boolean> removeLoginLogs(@RequestBody List<Long> ids) {
        return isOk(loginLogService.removeBatchByIds(ids));
    }

    @Operation(summary = "03.登录日志导出", description = "将符合条件的登录日志导出到excel")
    @ApiOperationLog(desc = "将符合条件的登录日志导出到excel", businessType = BusinessTypeEnum.EXPORT)
    @PostMapping("/login/export")
    public ResponseEntity<byte[]> loginLogsExcel(@RequestParam("fileName") String fileName,@RequestBody LoginLogQuery query) {
        List<SysLoginLogEntity> list = loginLogService.list(query.toQueryWrapper());
        return CustomExcelUtil.exportExcel(fileName, list, SysLoginLogEntity.class);
    }

    @Operation(summary = "04.删除所有登录日志")
    @ApiOperationLog(desc = "删除所有登录日志", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/login/all")
    public ResponseResult<Boolean> removeAllLoginLogs() {
        return isOk(loginLogService.remove(new QueryWrapper<>()));
    }

    /**
     * 获取操作日志列表
     **/
    @Operation(summary = "05.分页查询操作日志")
    @GetMapping("/operation")
    public ResponseResult<PageResult<OperationLogDTO>> getOperationLog(OperationLogQuery query) {
        startPage();
        List<SysOperationLogEntity> sysOperationLogs = operationLogService.list(query.toQueryWrapper());
        PageResult<OperationLogDTO> data = getData(sysOperationLogs, OperationLogDTO::new);
        return ResponseResult.success(data);
    }

    @Operation(summary = "06.删除操作日志")
    @ApiOperationLog(desc = "删除操作日志", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/operation")
    public ResponseResult<Boolean> removeOperationLogs(@RequestBody List<Long> operationIds) {
        return isOk(operationLogService.removeBatchByIds(operationIds));
    }

    @Operation(summary = "07.操作日志导出", description = "将符合条件的操作日志全部导出到excel")
    @ApiOperationLog(desc = "将符合条件的操作日志全部导出到excel", businessType = BusinessTypeEnum.EXPORT)
    @PostMapping("/operation/export")
    public ResponseEntity<byte[]> operationLogsExcel(@RequestParam("fileName") String fileName,@RequestBody OperationLogQuery query) {
        List<SysOperationLogEntity> list = operationLogService.list(query.toQueryWrapper());
        List<OperationLogDTO> data = list.stream().map(OperationLogDTO::new).toList();
        return CustomExcelUtil.exportExcel(fileName, data, OperationLogDTO.class);
    }

    @Operation(summary = "08.删除所有操作日志")
    @ApiOperationLog(desc = "删除所有操作日志", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/operation/all")
    public ResponseResult<Boolean> removeAllOperationLogs() {
        return isOk(operationLogService.remove(new QueryWrapper<>()));
    }
}
